using System;

namespace SBPweb.Forge.Modules.Dependencies
{
	/// <summary>
	/// Class of VersionMask
	/// </summary>
    public class VersionMask
    {

        #region Private fields

        private int? major;
        private int? minor;
        private int? revision;
        private int? build;

        #endregion

        #region Contstructor logic

		/// <summary>
		/// Constructor of VersionMask
		/// </summary>
		/// <param name="version"></param>
        public VersionMask(string version)
        {
            string[] split = version.Split(new char[] { '.' }, StringSplitOptions.None);

            if (split.Length > 1)
            {
                major = TryGetInt(split[0]);
                minor = TryGetInt(split[1]);
            }
            if (split.Length > 2)
            {
                revision = TryGetInt(split[2]);
            }
            if (split.Length > 3)
            {
                build = TryGetInt(split[3]);
            }

        }

        #endregion

        #region Property accessors

		/// <summary>
		/// Major number
		/// </summary>
        public int? Major
        {
            get
            {
                return major;
            }
        }

		/// <summary>
		/// Minor number
		/// </summary>
        public int? Minor
        {
            get
            {
                return minor;
            }
        }

		/// <summary>
		/// Revision number
		/// </summary>
        public int? Revision
        {
            get
            {
                return revision;
            }
        }

		/// <summary>
		/// Bild number
		/// </summary>
        public int? Build
        {
            get
            {
                return build;
            }
        }

        #endregion

        #region Public methods

		/// <summary>
		/// ToString
		/// </summary>
		/// <returns>string</returns>
        public override string ToString()
        {
            return string.Format("{0}.{1}.{2}.{3}", major.HasValue ? major.Value.ToString() : "*", minor.HasValue ? minor.Value.ToString() : "*", revision.HasValue ? revision.Value.ToString() : "*", build.HasValue ? build.Value.ToString() : "*");
        }

		/// <summary>
		/// Is met?
		/// </summary>
		/// <param name="version"></param>
		/// <returns></returns>
        public bool IsMet(System.Version version)
        {
			return !(
				(major.HasValue && major.Value != version.Major)
				||
				(minor.HasValue && minor.Value != version.Minor)
				||
				(revision.HasValue && revision.Value != version.Revision)
				||
				(build.HasValue && build.Value != version.Build)
				);
        }

        #endregion

        #region Private helper functions

        private int? TryGetInt(string s)
        {
            try
            {
                if (s == "*")
                {
                    return null;
                }
                return Convert.ToInt16(s);
            }
            catch
            {
                return null;
            }
        }

        #endregion

    }
}
